const app = getApp();

// pages/handleAddress/handleAddress.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hide: app.globalData.hide,
    edit: false,
    address: [],
    username: "",
    tel: "",
    area: "",
  },

  chooseAddress() {
    const that = this;
    wx.chooseAddress({
      success(res) {
        that.setData({
          username: res.userName,
          tel: res.telNumber,
          area:
            res.provinceName +
            " " +
            res.cityName +
            " " +
            res.countyName +
            " " +
            res.detailInfo,
        });
        console.log(res);
      },
    });
  },

  formSubmit(e) {
    console.log("form发生了submit事件，携带数据为：", e.detail.value);
  },

  addAddress() {
    let { address, username, tel, area } = this.data;
    if (!username || !tel || !area) {
      wx.showModal({
        title: "提示",
        content: "请输入完整的收件人信息",
        showCancel: false,
        confirmText: "知道了",
        confirmColor: "#1296DB",
        success: (result) => {
          if (result.confirm) {
          }
        },
        fail: () => { },
        complete: () => { },
      });
      return;
    }
    const newAddewss = {
      userName: username,
      telNumber: tel,
      detailInfo: area,
    };
    address.push(newAddewss);
    wx.setStorageSync("address", address);
    wx.navigateBack({
      delta: 1,
    });
    wx.showToast({
      title: "添加成功",
      icon: "success",
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.id) {
      let data = {
        username: "张三",
        tel: "187363xxxxx",
        area: "广东省 广州市 海珠区 新港xxxxxx",
      };
      this.setData({
        edit: true,
        username: data.username,
        tel: data.tel,
        area: data.area,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const address = wx.getStorageSync("address") || [];
    this.setData({
      address,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
